1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.testing;
18
19 import junit.framework.AssertionFailedError;
20 import junit.framework.TestCase;
21
22 import java.io.Serializable;
23
24
25
26
27
28
29 public class SerializableTesterTest extends TestCase {
30 public void testStringAssertions() {
31 String original = "hello world";
32 String copy = SerializableTester.reserializeAndAssert(original);
33 assertEquals(original, copy);
34 assertNotSame(original, copy);
35 }
36
37 public void testClassWhichDoesNotImplementEquals() {
38 ClassWhichDoesNotImplementEquals orig =
39 new ClassWhichDoesNotImplementEquals();
40 boolean errorNotThrown = false;
41 try {
42 SerializableTester.reserializeAndAssert(orig);
43 errorNotThrown = true;
44 } catch (AssertionFailedError error) {
45
46 assertContains("must be Object#equals to", error.getMessage());
47 }
48 assertFalse(errorNotThrown);
49 }
50
51 public void testClassWhichIsAlwaysEqualButHasDifferentHashcodes() {
52 ClassWhichIsAlwaysEqualButHasDifferentHashcodes orig =
53 new ClassWhichIsAlwaysEqualButHasDifferentHashcodes();
54 boolean errorNotThrown = false;
55 try {
56 SerializableTester.reserializeAndAssert(orig);
57 errorNotThrown = true;
58 } catch (AssertionFailedError error) {
59
60 assertContains("must be equal to the Object#hashCode", error.getMessage());
61 }
62 assertFalse(errorNotThrown);
63 }
64
65 public void testObjectWhichIsEqualButChangesClass() {
66 ObjectWhichIsEqualButChangesClass orig =
67 new ObjectWhichIsEqualButChangesClass();
68 boolean errorNotThrown = false;
69 try {
70 SerializableTester.reserializeAndAssert(orig);
71 errorNotThrown = true;
72 } catch (AssertionFailedError error) {
73
74 assertContains("expected:<class ", error.getMessage());
75 }
76 assertFalse(errorNotThrown);
77 }
78
79 private static class ClassWhichDoesNotImplementEquals
80 implements Serializable {
81 private static final long serialVersionUID = 1L;
82 }
83
84 private static class ClassWhichIsAlwaysEqualButHasDifferentHashcodes
85 implements Serializable {
86 private static final long serialVersionUID = 2L;
87
88 @Override
89 public boolean equals(Object other) {
90 return (other instanceof ClassWhichIsAlwaysEqualButHasDifferentHashcodes);
91 }
92 }
93
94 private static class ObjectWhichIsEqualButChangesClass
95 implements Serializable {
96 private static final long serialVersionUID = 1L;
97
98 @Override
99 public boolean equals(Object other) {
100 return (other instanceof ObjectWhichIsEqualButChangesClass
101 || other instanceof OtherForm);
102 }
103
104 @Override
105 public int hashCode() {
106 return 1;
107 }
108
109 private Object writeReplace() {
110 return new OtherForm();
111 }
112
113 private static class OtherForm implements Serializable {
114 @Override
115 public boolean equals(Object other) {
116 return (other instanceof ObjectWhichIsEqualButChangesClass
117 || other instanceof OtherForm);
118 }
119
120 @Override
121 public int hashCode() {
122 return 1;
123 }
124 }
125 }
126
127 private static void assertContains(String expectedSubstring, String actual) {
128
129 if (!actual.contains(expectedSubstring)) {
130 fail("expected <" + actual + "> to contain <" + expectedSubstring + ">");
131 }
132 }
133 }